home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / STRX.ZIP / STRSEARC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-14  |  4.6 KB  |  206 lines

  1. //
  2. // strsearch.cpp: str class member functions for searching and replacing
  3. // Author       : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. //
  10. // Version 2.4      12/16/93
  11. //    Support reverse searcing
  12. //
  13. // Version 2.2      5/5/93
  14. //    Fix bug in index relating to case sensitivity (backwards)
  15. //
  16. // Version 2.00     11/31/92
  17. //    Support searching/replacing, regular expressions, case sensitivity
  18. //
  19.  
  20. #include <string.h>
  21. #include <limits.h>
  22.  
  23. #include "str.h"
  24.  
  25. inline int min(int x, int y){if (x<y) return x; else return y;}
  26.  
  27. // 
  28. // index member functions
  29. //
  30. int str::index(const char * s, int start) const{
  31.    if (!caseSensitive()){
  32.       str mylowstr(::lowercase(*this));
  33.       const char * charptr = strstr(mylowstr(start), ::lowercase(s));
  34.       if (charptr) return (charptr - mylowstr());
  35.       else return (-1);
  36.    }
  37.    else {
  38.       const char * charptr = strstr((*this)(start), s);
  39.       if (charptr) return (charptr - data->buf);
  40.       else return (-1);
  41.    }
  42. };
  43.  
  44. // Search backwards
  45. int str::indexr(const char * s, int start) const{
  46.    int len1=length();
  47.    int len2=strlen(s);
  48.  
  49.    if (len1<len2) return -1;
  50.    int pos = len1-len2;
  51.    if (start>=0) pos = min(pos, start);
  52.  
  53.    while (pos>=0)
  54.    {
  55.      if (!strncmp(data->buf+pos, s, len2)) return pos;
  56.      pos--;
  57.    }
  58.    return -1;
  59. };
  60.  
  61. int str::index(char s, int start) const
  62. {
  63.     const char * pos = strchr((*this)(start), s);
  64.    if (pos) return pos-data->buf;
  65.    else return -1;
  66. }
  67.  
  68. int str::indexr(char s, int start) const
  69. {
  70.    while (start>=0){
  71.      if (data->buf[start]==s) return start;
  72.      else start--;
  73.    }
  74.    return start;
  75. }
  76.  
  77.  
  78. //
  79. // search operator functions
  80. //
  81.  
  82. int str::search(const char * s, int* startPtr) const{
  83.    *startPtr = index(s, *startPtr);
  84.    return *startPtr>=0;
  85. };
  86.  
  87. int str::search(const char * s, int start) const{
  88.    return index(s, start)>=0;
  89. };
  90.  
  91. int str::search(char s, int* startPtr) const{
  92.    *startPtr = index(s, *startPtr);
  93.     return *startPtr>=0;
  94. };
  95.  
  96. int str::search(char s, int start) const{
  97.    return index(s, start);
  98. };
  99.  
  100. int str::searchr(char s, int* startPtr) const{
  101.    *startPtr = indexr(s, *startPtr);
  102.     return *startPtr>=0;
  103. };
  104.  
  105. int str::searchr(char s, int start) const{
  106.    return indexr(s, start);
  107. };
  108.  
  109.  
  110. int str::searchr(const char * s, int* startPtr) const{
  111.    *startPtr = indexr(s, *startPtr);
  112.    return *startPtr>=0;
  113. };
  114.  
  115. int str::searchr(const char * s, int start) const{
  116.    return indexr(s, start)>=0;
  117. };
  118.  
  119. int str::replace(const char * searchStr, const char* replaceStr,
  120.                  int start, int numReplacements)
  121. {
  122.   return replace(searchStr, replaceStr, &start, numReplacements);
  123. };
  124.  
  125.  
  126. int str::replace(const char * searchStr, const char* replaceStr,
  127.                  int* startPtr, int numReplacements)
  128. {
  129.  
  130.    if (numReplacements==0) return 0;
  131.  
  132.    int& start = *startPtr;
  133.    int countReplacements=0;
  134.    int searchLen = strlen(searchStr), replaceLen = strlen(replaceStr);
  135.  
  136.    do {
  137.  
  138.       start = index(searchStr, start);
  139.       if (start<0) break;
  140.       countReplacements++;
  141.       operator()(start, searchLen) = replaceStr;
  142.       start+= replaceLen;           //skip past newly added data
  143.  
  144.       if (!--numReplacements) break;
  145.  
  146.    } while (1);
  147.  
  148.    return countReplacements;
  149.  
  150. };
  151.  
  152.  
  153. int str::replaceAll(const char * searchStr, const char* replaceStr, int start)
  154. {
  155.   return replace(searchStr, replaceStr, &start, INT_MAX);
  156. };
  157.  
  158. int str::count(const char * searchStr, int start)
  159. {
  160.     int countReplacements=0;
  161.    int searchLen = strlen(searchStr);
  162.  
  163.     while (search(searchStr, start))
  164.    {
  165.         start+= searchLen;
  166.         countReplacements++;
  167.    }
  168.  
  169.     return countReplacements;
  170. };
  171.  
  172. int str::replacer(const char * searchStr, const char* replaceStr,
  173.                  int start, int numReplacements)
  174. {
  175.   return replacer(searchStr, replaceStr, &start, numReplacements);
  176. };
  177.  
  178. int str::replacer(const char * searchStr, const char* replaceStr,
  179.                  int* startPtr, int numReplacements)
  180. {
  181.  
  182.    if (numReplacements==0) return 0;
  183.  
  184.    int& start = *startPtr;
  185.    int countReplacements=0;
  186.    int searchLen = strlen(searchStr);
  187.  
  188.    do {
  189.  
  190.       start = indexr(searchStr, start);
  191.       if (start<0) break;
  192.       countReplacements++;
  193.       operator()(start, searchLen) = replaceStr;
  194.       if (start) start--;           //skip past newly added data
  195.       else break;
  196.  
  197.       if (!--numReplacements) break;
  198.  
  199.    } while (1);
  200.  
  201.    return countReplacements;
  202.  
  203. };
  204.  
  205.  
  206.